/*
策略出处: https://www.botvs.com/strategy/8916
策略名称: 记录净资产变动情况2.0
策略作者: 青铜战国级机器人
策略描述:

说明
- 本策略由https://www.botvs.com/strategy/5349 演变而来，主要是计算在含有借币的情况下，账户策略的净收入变动情况
- 净资产变动： 账户净资产变动情况
- 状态信息栏会显示如下信息：
   - 初始净资产 / 初始净钱 / 初始净币: 显示最开始时的净资产，净钱币情况
   - 当前净资产 / 当前净钱 / 当前净币：如果正在做多显示红色，如果正在做空显示绿色
  -  净资产变化 / 总收益 / 月收益 / 最大回撤：净资产较上一次上涨则显示红色，较上一次下降则显示绿色
  - 借钱 / 借钱利息：
  - 借币 / 借币利息
@time: 每隔 time 小时通知一次“月收益，净钱，净币，借币，现在所欠利息”信息；净币为正说明在做多，净币为负说明在做空
@shuaxintime： 每隔shuaxintime分钟后通知一次账户净资产变动情况
@profit_base: 开始时净资产的变动情况
@borrow_bi: 开始时借币总数
@interest_bi_base: 开始时借币欠下的总利息
@interest_bi: 借币利息，默认0.001

-TODO
直接调用借款信息，计算净资产变化情况


参数                  默认值    描述
------------------  -----  ---------
time                true   通知周期（小时）
shuaxintime         30     刷新周期（分钟）
profit_base         false  初始净资产变动值
borrow_bi           false  借币数量
interest_bi_base    false  借币现在所欠利息
interest_bi         0.001  借币日利息
borrow_qian         false  借钱数目
interest_qian       0.001  借钱日利息
interest_qian_base  false  借钱现在所欠利息
log_reset           false  log reset
*/

function EnsureCall(e, method) {
    var r;
    while (!(r = e[method].apply(this, Array.prototype.slice.call(arguments).slice(2)))) {
        Sleep(1500);
    }
    return r;
}

function adjustFloat(v) {
    return Math.floor(v*1000)/1000;
}

function main() {
    SetErrorFilter("502:|503:|unexpected|network|timeout|WSARecv|Connect|GetAddr|no such|reset|http|received|EOF");
    var zhouqi = time * 3600000;
    var shuaxin = shuaxintime * 60000;
    var time0 = new Date().getTime();
    var time_mail = time0 + zhouqi - 5000;
    var time1 = time0 + shuaxin - 5000;
    var accounts = new Array([exchanges.length]);
    var tickers = new Array([exchanges.length]);
    var qian = [0, 0];
    var bi = [0, 0];
    var bi_money = [0, 0];
    var pnow = 0;
    var profit = profit_base;
    var vv = 0;
    var lv = profit;
    var msg = "暂时没有数据";
    var n = 0;
    var history = 0;
    var last_history = 0;
    var max_history = 0;
    var min_history = 0;
    var max_down = 0;
    
    var net_msg = 0;
    var profit_msg = 0;
    var bi_interest_msg = 0;
    var qian_interest_msg = 0;
    var init_profit_msg = 0;
    var status_log= 0;
    var red_color = "#ff0000";
    var green_color = "#006600";
    
    
    exchanges[0].SetLimit(1000);
    accounts[0] = EnsureCall(exchanges[0], "GetAccount");
    Sleep(2000);
    tickers[0] = EnsureCall(exchanges[0], "GetTicker");
    Sleep(2000);
    if (log_reset) {
        LogProfitReset(); 
    }
    qian[0] = accounts[0].Balance + accounts[0].FrozenBalance - borrow_qian - interest_qian_base;
    bi[0] = accounts[0].Stocks + accounts[0].FrozenStocks - borrow_bi - interest_bi_base;
    bi_money[0] = bi[0] * tickers[0].Last;
    net0 = qian[0] + bi_money[0];
    msg = "初始净资产 / 初始净钱 / 初始净币：" + net0 + " / " + qian[0] + " / " + bi[0] + "@";
    init_profit_msg = "初始净资产 / 初始净钱 / 初始净币：" + net0 + " / " + qian[0] + " / " + bi[0];
    LogProfit(lv, msg);
    LogStatus(msg);
    LogProfit(lv, "所有账户初始数据已处理完毕，等待通知……@");
    while (true) {
        if (new Date().getTime() > time1) {
            vv = profit;
            qian[1] = 0;
            bi[1] = 0;
            bi_money[1] = 0;
            profit = profit_base;
            lv = 0;
            exchanges[0].SetLimit(1000);
            accounts[0] = EnsureCall(exchanges[0], "GetAccount");
            Sleep(2000);
            tickers[0] = EnsureCall(exchanges[0], "GetTicker");
            Sleep(2000);
            qian[1] += accounts[0].Balance + accounts[0].FrozenBalance;
            interest_qian_all = interest_qian_base + borrow_qian * interest_qian * (new Date().getTime() - time0) / 86400000;
            borrow_qian_all = borrow_qian + interest_qian_all;
            qian[1] = qian[1] - borrow_qian_all;
            bi[1] = accounts[0].Stocks + accounts[0].FrozenStocks;
            interest_bi_all = interest_bi_base + borrow_bi * interest_bi * (new Date().getTime() - time0) / 86400000;
            borrow_bi_all = borrow_bi + interest_bi_all;
            bi[1] = bi[1] - borrow_bi_all;
            bi_money[1] = bi[1] * tickers[0].Last;
            profit += qian[1] + bi_money[1] - net0;
            lv = adjustFloat(profit * 259200000000 / ((net0) * (new Date().getTime() - time0)));
            history = Math.round(profit * 1000) / 1000;
            max_history = Math.max(history, max_history);
            if (max_history == history) {
                min_history = max_history;
            }
            min_history = Math.min(history, min_history);
            var present_maxdown = (min_history - max_history) / (max_history + net0) * 100;
            max_down = adjustFloat(Math.min(present_maxdown, max_down));
            var lv_total = adjustFloat(profit / net0 * 100);
            
            net_msg = "当前净资产 / 当前净钱 / 当前净币：" + (qian[1] + bi_money[1]) + " / " + qian[1] + " / " + bi[1];
            if (bi[1] > 0) {
                net_msg += red_color;
            }
            else if (bi[1] < 0) {
                net_msg += green_color;
            }
            profit_msg = "净资产变化 / 总收益 / 月收益 / 最大回撤：" + history + " / " + lv_total + "% / " + lv + "% / " + max_down + "%";
            if (last_history < history) {
                profit_msg += red_color;
            }
            else if (last_history > history) {
                profit_msg += green_color;
            }
            last_history = history;
            qian_interest_msg = "借钱 / 借钱利息：" + borrow_qian + " / " + interest_qian_all;
            bi_interest_msg = "借币 / 借币利息：" + borrow_bi + " / " + interest_bi_all;
            msg = "月收益 / 净钱 / 净币 / 净资产 / 借钱 / 借钱利息 / 借币 / 现在所欠利息：" + lv + " / " + qian[1] + " / " + bi[1] + " / " + (qian[1] + bi_money[1]) + " / " + borrow_qian + " / " + interest_qian_all + " / " + borrow_bi + " / " + interest_bi_all;
            if (new Date().getTime() > time_mail) {
                n += 1;
                LogProfit(history, msg);
                time_mail += zhouqi;
            } else {
                LogProfit(history, "净资产变动");
            }
            time1 += shuaxin;
        }
        Sleep(5000);
        status_log = init_profit_msg + "\n" + net_msg + "\n" + profit_msg + "\n" + qian_interest_msg + "\n" + bi_interest_msg;
        LogStatus(status_log);
    }
}
